package com.yedean.factory.strategy;

import com.yedean.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Constructor;

/**
 * CglibInstantiationStrategy 
 *
 * @author yedean
 * @date 2022/7/5
 */
public class CglibInstantiationStrategy implements InstantiationStrategy {

    @Override
    public Object instantiate(BeanDefinition definition, String beanName, Constructor<?> ctor, Object[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(definition.getBeanClass());
        if (null==ctor){
            return enhancer.create();
        }
        return enhancer.create(ctor.getParameterTypes(),args);
    }
}
